package client.domainModels.NodeImpl;

import java.util.ArrayList;
import java.util.HashMap;

import GUI.view.CombBoxHistory;

import client.domainModels.Node.Node;
import client.domainModels.Node.NodePrintable;
import client.domainModels.Node.NodeView;

/**
 * 
 * @author Wei WANG
 */
public abstract class AbstractNode implements Node,NodePrintable,NodeView {

	public ArrayList<String> childNodeClassName = new ArrayList<String>();

	public HashMap<String, ArrayList<AbstractNode>> childNames_childObjs = new HashMap<String, ArrayList<AbstractNode>>();

	public ArrayList<String> nodeAttributes = new ArrayList<String>();

	private String ID = "";
	
	//private ArrayList<String> history = new ArrayList<String>();

	
	/*public void addRecordInHistory(String s){
		if(history.indexOf(s)==-1){
			history.add(s);
		}
	}
	
	public ArrayList<String> getHistoryList(){
		return history;
	}
	
	public String[] getHistorySet(){
		return (String[]) history.toArray();
	}*/
	public abstract ArrayList<String> getHistoryList();
	
	public abstract void addRecordInHistory(String s);
	
	public abstract String[] getHistorySet();
	
	public abstract String[] getChildNodeNames();

	public abstract String[] getNodeAttributes();
	
	public abstract String getStringRepresentation();

	public abstract String getRep();
	
	public ArrayList<AbstractNode> getChildNodes(String childClassName){
		return childNames_childObjs.get(childClassName);
	}

	public AbstractNode() {
		initMap();
	}

	// this method has to be called in the constructor
	public void initMap() {
		
		for (int i = 0; i < getChildNodeNames().length; i++) {
			ArrayList<AbstractNode> emptyList = new ArrayList<AbstractNode>();
			childNames_childObjs.put(getChildNodeNames()[i], emptyList);
		}
	}

	public boolean isleaf() {
		boolean b = true;
		if (getChildNodeNames().length > 0) {
			b = false;
		}
		return b;
	}

	public String getID() {
		return ID;
	}

	public void setID(String s) {
		this.ID = s;
	}

	public void addChildNode(AbstractNode child) {
		String childClassName = child.getClass().getSimpleName();

		ArrayList<AbstractNode> listChildren = childNames_childObjs.get(childClassName);
		listChildren.add(child);
		childNames_childObjs.put(childClassName, listChildren);

	}

	public void removeChildNode(AbstractNode child) {
		String childClassName = child.getClass().getSimpleName();
		ArrayList<AbstractNode> listchild = childNames_childObjs.get(childClassName);
		int index = listchild.indexOf(child);
		if (index >= 0) {
			listchild.remove(index);
			childNames_childObjs.put(childClassName, listchild);
		}
	}

	public void removeAllChildren(){
		String[] childrenTypes = getChildNodeNames();
		for(int i=0;i<childrenTypes.length;i++){
			getChildNames_childObjs().get(childrenTypes[i]).clear();
		}
	}
	
	public abstract String toString();

	public HashMap<String, ArrayList<AbstractNode>> getChildNames_childObjs() {
		return childNames_childObjs;
	}
	
	public void setChildNames_childObjs(HashMap<String, ArrayList<AbstractNode>> set){
		childNames_childObjs = set;
	}
	
	
}
